<?php
/**
 * 控制接口
 *
 *
 */
 namespace OM\Api;

use OM\Lib\Apihelper;
use OM\Dev\Ext;
use OM\Dev\Trunk;
use OM\Dev\Group;

 class Control
 {
 	/**
 	 * 查询设备信息
 	 *
 	 * @return  array 返回查询信息
 	 */
 	public function queryDevice( $om )
 	{
 		$attr = new \stdClass();
 		$attr->DeviceInfo = null;
 		$apiXml = Apihelper::_query('Query', $attr );
 		$resXml = $om->Apiagent( $apiXml );
 		$dinfo =  Apihelper::parseXML( $resXml );
 		$arrDevices = false;
 		if( $dinfo ){
 			if( isset( $dinfo->manufacturer ) && $dinfo->manufacturer )
 				$arrDevices['manufacturer'] = $dinfo->manufacturer;
 			if( isset( $dinfo->mac ) && $dinfo->mac )
 				$arrDevices['mac'] = $dinfo->mac;
 			if( isset( $dinfo->version ) && $dinfo->version )
 				$arrDevices['version'] = $dinfo->version;
 			if( isset( $dinfo->model ) && $dinfo->model )
 				$arrDevices['model'] = $dinfo->model;
 			if( isset( $dinfo->devices) ){
 				$devices =  $dinfo->devices;
 				//模拟分机
 				if( count($devices->ext)  ){
 					$arrDevices['exts'] = array();
 					foreach( $devices->ext as $vext ){
	 					$extID = (string)$vext['id'];
	 					$item = new Ext();
	 					$item->lineid = (string)$vext['lineid'];
	 					$item->id = (string)$vext['id'];
	 					$arrDevices['exts'][$extID]  = $item;
	 				}
 				}
 				//中继
 				if(  count($devices->line) ){
 					$arrDevices['trunks'] = array();
 					foreach( $devices->line as $vline ){
	 					$extID = (string)$vline['id'];
	 					$item = new Trunk();
	 					$item->lineid = (string)$vline['lineid'];
	 					$item->id= $vline['id'];
	 					$arrDevices['trunks'][$extID]  = $item;
	 				}
 				}
 				//分组
 				if(  count($devices->group) )
 				{
 					$arrDevices['groups'] = array();
 					foreach( $devices->group as $vgroup ){
	 					$extID = (string)$vgroup['id'];
	 					$item = new Group();
	 					$item->exts =  array();
	 					$item->id = $vgroup['id'];
	 					foreach( $vgroup->ext as $vvext ){
	 						$id = (string) $vvext['id'];
	 						$items->exts[$id] = $id;
	 					}
	 					$arrDevices['groups'][$extID]  = $item;
	 				}
 				}
 			}
 		}
		return $arrDevices;
 	}

 	public function queryExt( $_om, $_extID )
 	{
 		if( !$_om || !$_extID )
 			return false;
 		$attr = new \stdClass();
 		$attr->ext = array('id'=>$_extID );
 		$apiXml = Apihelper::_query('Query', $attr );
 		$resXml = $_om->Apiagent( $apiXml );
 		$extinfo =  Apihelper::parseXML( $resXml );
 		$parseRes = false;
 		if( $extinfo ){
 			if( isset( $extinfo->ext ) ){
 				$parseRes = array();
 				$xmlExt = $extinfo->ext;
 				$parseRes['id'] = intval( $xmlExt['id'] );
 				$parseRes['lineid'] = strval( $xmlExt->lineid );
 				if( isset( $xmlExt->staffid ) )
 				$parseRes['staffid'] = (string) $xmlExt->staffid;
 				$parseRes['groups']  = array();
 				if( isset( $xmlExt->group  ) ) {
 					foreach( $xmlExt->group as $vgroup ){
 						$parseRes['groups'][] = (int) $vgroup['id'];
 					}
 				}
 				if( isset( $xmlExt->Call_Pickup ) )
 					$parseRes['call_picup'] = (string)$xmlExt->Call_Pickup;
 				if( isset( $xmlExt->Call_Restriction )  )
 					$parseRes['call_region'] = (int)$xmlExt->Call_Restriction;
 				if(  isset( $xmlExt->Fwd_Number) )
 					$parseRes['voicefile'] = (string)$xmlExt->voicefile;
 				if( isset( $xmlExt->mobile)  )
 					$parseRes['mobile'] = (string)$xmlExt->mobile;
 				if( isset($xmlExt->Fwd_Type )  )
 					$parseRes['fwd_type'] = (string)$xmlExt->Fwd_Type;
 				if( isset( $xmlExt->Fwd_Number)  )
 					$parseRes['fwd_number'] =  (string)$xmlExt->Fwd_Number;
 				if(  isset( $xmlExt->fork )  )
 					$parseRes['fork'] = (string)$xmlExt->fork;
 				if( isset($xmlExt->record) )
 					$parseRes['record'] = (string)$xmlExt->record;
 				$parseRes['api'] = (int)$xmlExt->api;
 				//判断分机状态
 				$extState = (string)$xmlExt->state;
 				$extState = strtolower( $extState );
 				if( 'ready' == $extState && !isset( $xmlExt->outer) && !isset( $xmlExt->visitor) && !isset( $xmlExt->ext) ){
 					$parseRes['state'] = Ext::EXT_READY;
 				}
 				elseif('active' == $extState)
 					$parseRes['state'] = Ext::EXT_ACTIVE;
 				elseif('progress' == $extState)
 					$parseRes['state'] = Ext::EXT_PROGRESS;
 				else
 					$parseRes['state'] = Ext::EXT_BUSY;
 			}
 		}

 		return $parseRes;
 	}

 	public function queryTrunk($_om,$_extID)
 	{
 		//查询中继号
		if( !$_om || !$_extID )
			return false;
 		$attr = new \stdClass();
 		$attr->trunk = array('id'=>$_extID );

 		$apiXml = Apihelper::_query('Query', $attr );
 		$resXml = $_om->Apiagent( $apiXml );
 		$trunkinfo =  Apihelper::parseXML( $resXml );
 		$parseRes = false;
 		if( $trunkinfo )
 		{
 			if( isset( $trunkinfo->trunk ) )
 			{
 				$parseRes = true;
 				$xmlExt = $trunkinfo->trunk;
 				$trunk->lineid = (string) $xmlExt->lineid;

 				//判断状态
 				$trunkState = (string)$xmlExt->state;
 				$trunkState = strtolower( $trunkState );
 				if( 'ready' == $trunkState && !isset( $xmlExt->visitor) && !isset( $xmlExt->trunk) )
 				{
 					$trunk->state = Trunk::TRUNK_READY;
 				}
 				elseif('active' == $trunkState){
 					$trunk->state = Trunk::TRUNK_ACTIVE;
 				}
 				elseif('unwired' == $trunkState){
 					$trunk->state = Trunk::TRUNK_UNWIRED;
 				}
 				else
 					$trunk->state = Trunk::TRUNK_OFFLINE;
 			}
 		}

 		return $parseRes ? $trunk : false ;
 	}

 	/**
 	 * 挂断通话
 	 * @param object $_om      OM对象
 	 * @param string $_extID   分机号
 	 * @param string $_visitor 来访通话ID
 	 * @param string $_outer   外拨电话ID
 	 */
 	public function Clear($_om , $_extID, $_visitor = null , $_outer = null)
 	{
 		if( !$_om || !$_extID )
 			return false;
 		$attr = new \stdClass();
 		if( $_visitor ){
 			$attr->visitor = array('id'=>$_visitor );
 		}
 		else if( $_outer ){
 			$attr->outer = array('id'=>$_outer );
 		}
 		else
 			$attr->ext = array('id'=>$_extID );
 		$apiXml = Apihelper::_query('Clear', $attr );
 		$resXml = $_om->Apiagent( $apiXml );
 		$extinfo =  Apihelper::parseXML( $resXml );
 		return 1;
 	}
 	public function Incoming($_om,$num)
 	{
 		//查询来电
 		$attr = new \stdClass();
 		$attr->visitor = array('id'=>$num );
 		$apiXml = Apihelper::_query('Query', $attr );
 		$resXml = $_om->Apiagent( $apiXml );
 		$Incominfo =  Apihelper::parseXML( $resXml );
 		if($Incominfo)
 		{
 			if( isset( $Incominfo->visitor ))
 			{
 				$xmlExt = $Incominfo->visitor;
 				$visitor->id = (string) $xmlExt->id;
 				$visitor->state = (string) $xmlExt->state;
 			}
 		}
 		return $visitor;
 	}

 	public function Mute($_om,$_extID)
 	{
 		//静音模式
 		$attr = new \stdClass();
		$attr->ext = array('id'=> $_extID );

 		$apiXml = Apihelper::_query('Mute', $attr);
 		$resXml = $_om->Apiagent( $apiXml );
 		$Muteinfo =  Apihelper::parseXML( $resXml );
 		$parseRes = false;
 		if($Muteinfo)
 			return $parseRes ? '' : false ;
 	}

 	public function Monitor($_om,$_extID,$monitorId)
 	{
 		//监听
 		$attr1 = new \stdClass();
		$attr1->ext = array('id'=> $_extID );
 		$attr2 = new \stdClass();
		$attr2->ext = array('id'=> $monitorId );

		$apiXml = Apihelper::_query('Monitor',$attr1,$attr2,'Control');
 		$resXml = $_om->Apiagent( $apiXml );
 		$Monitorinfo =  Apihelper::parseXML( $resXml );
 		$parseRes = false;
 		if($Monitorinfo)
 			return $parseRes ? '' : false ;
 	}

 	public function Bargein($_om,$_extID,$bargeinId)
 	{
 		//强插
 		$attr1 = new \stdClass();
		$attr1->ext = array('id'=> $_extID );

 		$attr2 = new \stdClass();
		$attr2->ext = array('id'=> $bargeinId );

		$apiXml = Apihelper::_query('Bargein',$attr1, $attr2, 'Control');
 		$resXml = $_om->Apiagent( $apiXml );
 		$Bargeininfo =  Apihelper::parseXML( $resXml );
 		$parseRes = false;
 		if($Bargeininfo)
 			return $parseRes ? '' : false ;
 	}

 	public function assignMute($_om,$_strId,$_voice,$_repeat,$_length,$_exit)
 	{
 		//配置语音菜单

 		$attr1 = new \stdClass();
		$attr1->menu = array('id'=> $_strId );
		$attr2 = new \stdClass();
		$attr2->voicefile = $_voice;
		$attr3 = new \stdClass();
		$attr3->repeat = $_repeat;
		$attr4 = new \stdClass();
		$attr4->infolength = $_length;
		$attr5 = new \stdClass();
		$attr5->exit = $_exit;

 		$apiXml = Apihelper::_assign('Mute', $attr);
 		$resXml = $_om->Apiagent( $apiXml );
 		$AssignInfo =  Apihelper::parseXML( $resXml );
 		$arrAssign = false;
 		if($AssignInfo)
 		{
 			if(isset( $AssignInfo->menu) ){
 				$parseRes = array();
 				$xmlExt = $AssignInfo->menu;
 				if( isset( $xmlExt->voicefile ) )
 					$parseRes['voicefile'] = (string)$xmlExt->voicefile;
 				if( isset( $xmlExt->repeat )  )
 					$parseRes['repeat'] = (int)$xmlExt->repeat;
 				if(  isset( $xmlExt->infolength) )
 					$parseRes['infolength'] = (int)$xmlExt->infolength;
 				if( isset( $xmlExt->exit)  )
 					$parseRes['exit'] = (string)$xmlExt->exit;
 				if( isset( $xmlExt->outer))
 				{
 					$menu->outer->attributes()['id']  = (int)$xmlExt->outer->attributes()['id'];
 					$menu->outer->attributes()['from']  = (int)$xmlExt->outer->attributes()['from'];
 					$menu->outer->attributes()['to']  = (int)$xmlExt->outer->attributes()['to'];
 					$menu->outer->attributes()['trunk']  = (int)$xmlExt->outer->attributes()['trunk'];
 				}
 				if( isset( $xmlExt->visitor ))
 				{
 					$menu->visitor->attributes()['id']  = (int)$xmlExt->visitor->attributes()['id'];
 					$menu->visitor->attributes()['from']  = (int)$xmlExt->visitor->attributes()['from'];
 					$menu->visitor->attributes()['to']  = (int)$xmlExt->visitor->attributes()['to'];
 				}
 			}
 		}
 		return $arrAssign;
 	}

 	/**
 	 * 来电转接
 	 *
 	 * @param  [type] $_om    OM 代理对象
 	 * @param  [type] $_attr1 参数1，stdObject
 	 * @param  [type] $_att2  参数2，stdObject
 	 * @return [type]         转接结果
 	 */
 	private function _transfer( $_om, $_attr1, $_att2 )
 	{
 		$apiXml = Apihelper::_query('Connect', $_attr1, $_att2,'Transfer' );
 		$resXml = $_om->Apiagent( $apiXml );
 		$Transferinfo =  Apihelper::parseXML( $resXml );
 		$event = null;
 		if($Transferinfo)
 		{
 			if( isset( $Transferinfo->event  ) )
			{
				$event = array();
				$xmlExt = $Transferinfo->event;
				$event = $xmlExt ? reset( $xmlExt->attributes() ) : 1;
				$event = false;
			}
			elseif( isset( $Transferinfo->visitor)){
				$tmp = $Transferinfo->visitor;
				$event  =  reset( $tmp );
			}
 		}
 		return  $event;
 	}

 	/**
 	 * 将电话转接到外部电话
 	 *
 	 * @param class   $_om      OM代理对象
 	 * @param stdclas $_visitor 来电编号
 	 * @param stdclas $_outer   转接外部电话对象
 	 *
 	 *　@return null/array   转接信息
 	 */
 	public function TransVisitor2Outer( $_om, $_visitor, $_outer)
 	{
 		$attr1 = new \stdClass();
		$attr1->visitor = array('id'=> $_visitor );

		$attr2= new \stdClass();
		$attr2->outer = array('to'=>$_outer );

 		return $this->_transfer( $_om, $attr1, $attr2 );
 	}
 	/**
 	 * 来电转接到分机
 	 *
 	 * 先将来电保存,然后转接
 	 * @param [type] $_om      [description]
 	 * @param [type] $_visitor [description]
 	 * @param [type] $_ext     [description]
 	 */
 	public function TransVisitor2Ext( $_om, $_visitor, $_ext)
 	{
 		$attr1 = new \stdClass();
		$attr1->visitor = array('id'=> $_visitor );

		$attr2= new \stdClass();
		$attr2->ext = array('id'=>$_ext );
 		return $this->_transfer( $_om, $attr1, $attr2 );
 	}

 	public function Dail( $_om, $_extID, $phone_num )
 	{
 		//拨打电话
 		$attr1 = new \stdClass();
		$attr1->ext = array('id'=> $_extID );

		$attr2= new \stdClass();
		$attr2->outer = array('to'=>$phone_num );

		$apiXml = Apihelper::_query('Connect', $attr1, $attr2,'Transfer' );
 		$resXml = $_om->Apiagent( $apiXml );
 		$calldata =  Apihelper::parseXML( $resXml );
 		if( $calldata && 'TRANSIENT' == $calldata->attributes()['attribute'] ){
 			return 1;
 		}
 		return 0;
 	}

 }
?>